<?php

class Admin_IndexController extends Kieumau_Controller_Admin
{

    public function init()
    {
    	parent::init();
     /* Initialize action controller here */
 /* Initialize action controller here */
//     	$layoutPath = PUBLIC_PATH . '/templates/admin/default';
//     	$options = array ('layout' => 'index', 'layoutPath' => $layoutPath );
//     	Zend_Layout::startMvc ( $options );
//     	$albums_table = new Front_Model_DbTable_Albums ();
//     	$this->view->data = $albums_table->fetchAll ();

    	
    	
    }
    
    public function changelanguageAction(){
    	$this->_helper->layout->disableLayout ();
    	$this->_helper->viewRenderer->setNoRender ();
    	
    	if($this->getRequest()->isPost()){
    		$newCode = $this->getRequest()->getPost('code');
    		try{
	    		$languageTable = new Admin_Model_DbTable_Language();
	    		$languageTable->changeDefaultLanguage($newCode);
    		}catch(Exception $ex){
    			echo $ex->getMessage();
    		}
    	}
    	
    }
    
    

    public function indexAction()
    {
    	

    }
    
	public function loginAction()
    {
    	
    	
		// action body
    	if ($this->_request->isPost ()) {
    		$formData = $this->getRequest()->getPost();
    		
			// 1.Goi ket noi voi Zend Db
			$db = Zend_Registry::get ( 'connectDB' );
			// $db =
			// Zend_Db::factory($dbOption['adapter'],$dbOption['params']);
			
			// 2. Khoi tao Zend Autho
			$auth = Zend_Auth::getInstance ();
			
			// 3. Khai bao bang va 2 cot se su dung so
			// sanh trong qua trinh login
			$authAdapter = new Zend_Auth_Adapter_DbTable ( $db );
			$authAdapter->setTableName ( 'user' )->setIdentityColumn ( 'username' )->setCredentialColumn ( 'password' );
			
			// 4. Lay gia tri duoc gui qua tu FORM
			$uname = $this->_request->getParam ( 'username' );
			$paswd = $this->_request->getParam ( 'password' );
			
			// Nếu không tồn tại username hay password,
			// return
			if (empty ( $uname ) || empty ( $paswd )){
				return;
				//$this->_redirect ( '/admin/index/login' );
			}
				
				// 5. Dua vao so sanh voi du lieu khai
				// bao o muc 3
			$authAdapter->setIdentity ( $uname );
			//$authAdapter->setCredential ( md5 ( $paswd ) );
			$authAdapter->setCredential (  hash('sha512', $paswd ) );
			//$authAdapter->setCredential ( $paswd );
						
			
			// 6. Kiem tra trang thai cua user neu
			// status = 1 moi duoc login
			$select = $authAdapter->getDbSelect ();
			$select->where ( 'status = 1' );
			try{
			// 7. Lay ket qua truy van
			$result = $auth->authenticate ( $authAdapter );
			$flag = false;
			}catch(Exception $ex){
				echo "error: <br/> ".$ex->getMessage();
			}
			
			if ($result->isValid ()) {
				
				// 8. Lay nhung du lieu can thiet trong
				// bang users neu login thanh cong
				$data = $authAdapter->getResultRowObject ( null, array ('password' ) );
				
				// 9. Luu nhung du lieu cua member vao
				// session
				$auth->getStorage ()->write ( $data );
				
				$flag = true;
				$ns = new Zend_Session_Namespace ( 'adminSession' );	
				//$ns->logininfo= $auth->getIdentify();			
				
				require_once('Zend/Session.php');
				$rememberme = $this->_request->getParam ('rememberme');				
				$seconds  = 60 * 60 * 24 * 7; // 7 ngày
				if (isset($rememberme)) {					
					Zend_Session::RememberMe($seconds);
				}
				else {		
					Zend_Session::ForgetMe();
				}
				
				$ns->username = $data->username;
				$ns->password = $data->password;			
				$ns->id = $data->id;
			}
			
			if ($flag == true) {
				//Hủy session của employer
				//Zend_Session::namespaceUnset('employerntd');
				
				$this->_redirect ( 'admin/index' );
			} else//sai thông tin
				$this->_redirect ( 'admin/index/login' );
		} else {
			
			
		}
    }
    
    function addAction() {
    	$form = new Front_Form_Albums ();
    	$form->submit->setLabel ( 'Add' );
    	$this->view->myform = $form;
    	if ($this->getRequest ()->isPost ()) {
    		$formData = $this->getRequest ()->getPost ();
    		if ($form->isValid ( $formData )) {
    			$artist = $form->getValue ( 'artist' );
    			$title = $form->getValue ( 'title' );
    
    			$albums_table = new Front_Model_DbTable_Albums();
    			$albums_table->addAlbum ( $artist, $title );
    
    			$this->_helper->redirector ( 'index' );
    		} else {
    			$form->populate ( $formData );
    		}
    	}
    }
    
    function editAction(){
    	$form = new Front_Form_Albums();
    	$form->submit->setLabel('Save');
    	$this->view->myform = $form;
    	if($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		if($form->isValid($formData)){
    			$id = (int)$form->getValue('id');
    			$artist = $form->getValue('artist');
    			$title = $form->getValue('title');
    
    			$ablum_table = new Front_Model_DbTable_Albums();
    			$ablum_table->updateAlbum($id, $artist, $title);
    
    			$this->_helper->redirector('index');
    		}else {
    			$form->populate($formData);
    		}
    	}else {
    		$id = $this->_getParam('id',0);
    		if ($id > 0){
    			$ablum_table = new Front_Model_DbTable_Albums();
    			$form->populate($ablum_table->getAlbum($id));
    		}
    	}
    }
    
    function deleteAction(){
    	if($this->getRequest()->isPost()){
    		 
    		$del = $this->getRequest()->getPost('del');
    		if($del == 'Yes'){
    			$id = $this->getRequest()->getPost('id');
    			$albums = new Front_Model_DbTable_Albums();
    			$albums->deleteAlbum($id);
    		}
    		$this->_helper->redirector('index');
    		 
    	}else {
    		$id = $this->_getParam('id',0);
    		$albums = new Front_Model_DbTable_Albums();
    		$this->view->myform = $albums->getAlbum($id);
    	}
    }
    
    public function manageAction(){
    	$this->view->mainMenu = "Manage";
    	$this->view->subMenu = "";
    	
    }
    

}

